home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: tank.news.pipex.net!pipex!warwick!bsmail!talisker!nathan
- From: nathan@pact.srf.ac.uk (Nathan Sidwell)
- Subject: Re: Optimization Q: for( i=0; i<SIZE/2; i++) ??
- Message-ID: <Dn2nKB.C1@uns.bris.ac.uk>
- Sender: usenet@uns.bris.ac.uk (Usenet news owner)
- Nntp-Posting-Host: talisker.pact.srf.ac.uk
- Organization: Inmos
- X-Newsreader: TIN [version 1.2 PL2]
- References: <4g1j9n$ooe@news.csus.edu>
- Date: Tue, 20 Feb 1996 11:21:47 GMT
-
- Jerry Leong (wleong@sfsu.edu) wrote:
-
- : This question has been bothering me for quite a while now.
-
- : If I have the following for loop,
- : for( i=0; i< SIZE/2 ; i++).....
- : Will the code execute faster if I precompute SIZE/2 before hand and
- : do this
- : k = SIZE/2;
- : for(i=0; i<k ; i++).....
-
- : How does this work? I mean, would SIZE/2 get computed everytime during
- : the for loop, or it simply caculated once?
- It depends on
- a)what SIZE is
- b)your compiler
- c)the optimization level
-
- If SIZE is a simple #define, then the value of the expression 'SIZE/2'
- can be determined at compile time. This is called constant folding.
-
- Any modern complier, at its lowest, non-zero, level of optimization (ie
- -O1 or more), should work this out. so the code should compile to
- something like
-
- compareless <ctemp>,<i>,<SIZE/2>
- jumpnotzero <ctemp>,label
-
- If SIZE is not evaluable at compile time, then a compiler might be
- able to determine that the value of 'SIZE/2' does not change with loop
- iterations. It could then hoist the evaluation out of the loop, and
- therefore only do it once.
-
- Now, writing the loop as
- for(ix = SIZE/2; ix--;)
- ...
- might make a (small) difference.
-
- nathan
- --
- Nathan Sidwell Holder of the Xmris home page
- Chameleon Architecture Group at SGS-Thomson, formerly Inmos
- http://www.pact.srf.ac.uk/~nathan/ Tel 0117 9707182
- nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
-